<?php

class Users_Model_Users {

    private $_dbtable;

    public function __construct() {
        $this->_dbtable = new Users_Model_DbTable_Users();
    }

    /**
     * Returns all users from database
     * @return type
     */
    public function fetchAll() {
        $selectall = $this->_dbtable->select();
        return $this->_dbtable->fetchAll($selectall);
    }

    /**
     * Create new user
     * @param type $array
     */
    public function createUser($array) {
        $this->_dbtable->insert($array);
    }

    /**
     * Delete user
     * @param type $id
     */
    public function deleteUser($id) {
        $row = $this->_dbtable->fetchRow("id = $id");
        $row->delete();
    }

    /**
     * Update User
     * @param type $array
     * @param type $id
     */
    public function updateUser($array, $id) {
        $this->_dbtable->update($array, "id = $id");
    }

    /**
     * Get user by its id
     * @param type $id
     */
    public function getUser($id) {
        $select = $this->_dbtable->select();
        $select->where('id = ?', $id);
        return $this->_dbtable->fetchAll($select);
    }

    /**
     * get banned users
     */
    public function getBannedUsers() {
        $select = $this->_dbtable->select();
        $select->where('blocked = ?', true);
        return $this->_dbtable->fetchAll($select);
    }

    /**
     * Gets users based on their type
     * @param type $userType
     */
    public function getByUserType($userType) {
        $select = $this->_dbtable->select();
        $select->where('type = ? ', $userType);
        return $this->_dbtable->fetchAll($select);
    }

    public function getAllUsers() {
        $select = $this->_dbtable->select();
        $select->where('user_type = ?', 2);
        return $this->_dbtable->fetchAll($select);
    }

    /**
     * Returns valid users.
     * Banned = false
     */
    public function getValidUsers() {
        $select = $this->_dbtable->select();
        $select->where('confirm = ?', true)
                ->where('user_type = ?', 2);
        return $this->_dbtable->fetchAll($select);
    }

    public function getUsersByConfirmation($status) {
        $select = $this->_dbtable->select();
        $select->where('confirm = ?', $status)
                ->where('user_type = ?', 2);
        return $this->_dbtable->fetchAll($select);
    }

    /**
     * Validate user and  un-block status in database
     * @param type $array
     * @param type $validation_key
     */
    public function validateUser($array, $id) {
        $this->updateUser($array, $id);
    }

    public function getUserByValidationKey($validation_key) {
        $select = $this->_dbtable->select();
        $select->where('validation_key = ? ', $validation_key);
        return $this->_dbtable->fetchAll($select);
    }

    /**
     * Get Validated user by email
     * @param type $email
     * @return type
     */
    public function getValidUserByEmail($email) {
        $select = $this->_dbtable->select();
        $select->where('email = ?', $email);
        return $this->_dbtable->fetchAll($select);
    }

    /**
     * 
     * @param type $userId
     * @return type
     */
    public function getSearchSettingsByUserId($userId) {
        $user = $this->getUser($userId);
        return $user[0]['search_settings'];
    }

    public function getLastSearchByUserId($userId) {
        $user = $this->getUser($userId);
        return $user[0]['last_search'];
    }

    /**
     * 
     * @return type
     */
    public function todaysNewUsers() {
        $select = $this->_dbtable->select();
        $select->where('confirm = true')
                ->where('user_type = ?', 2)
                ->where('reg_date = ? ', date('Y-m-d', date('U')));
        return $this->_dbtable->fetchAll($select);
    }

}

